<template>
  <div class="category-dialog">
    <el-dialog :title="isEdit ? '更新分类' : '添加分类'" :visible="show" @close="$emit('changeDialog', false)" width="40%">
      <el-form class="custom-form" ref="form" :model="category" label-width="80px">
        <el-form-item class="custom-form-item" label="名称">
          <el-input v-model="category.name"></el-input>
        </el-form-item>
        <el-form-item class="custom-form-item" label="排序">
          <el-input v-model="category.sort"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('changeDialog', false)">取消</el-button>
        <el-button type="primary" @click="createResourceCategory"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { createOrUpdateCategory } from '@/apis/resource'
export default Vue.extend({
  name: 'create-or-update-category',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    category: {
      type: Object,
      default: function() {
        return { name: '', sort: 0, id: '' }
      }
    }
  },
  methods: {
    async createResourceCategory() {
      const { data } = await createOrUpdateCategory(this.category)
      if (data.code === '000000') {
        this.$message.success(this.isEdit ? '更新资源分类成功' : '添加资源分类成功')
        this.$emit('changeDialog', false, true)
      }
    }
  }
})
</script>
<style lang="scss" scoped>
.category-dialog {
  .custom-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .custom-form-item {
      width: 70%;
    }
  }
}
</style>
